package com.njlime.cms.repository.sys;

import com.njlime.cms.entities.LiveComment;
import com.njlime.core.base.repository.impl.BaseDaoImpl;
import com.njlime.core.helper.PageBean;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 直播评论
 * Created by  Dean on 2017/8/11.
 */
@Repository
public class LiveCommentDaoImpl extends BaseDaoImpl<LiveComment> {

    /**
     * 删除
     * @param id
     * @return
     */
    public int del(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" update t_live_comment set is_del = 1 where id = ?");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 分页列表
     * @param pageBean
     * @param comment
     * @return
     */
    public PageBean list(PageBean pageBean, LiveComment comment) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append("select * from t_live_comment ");
        sb.append(" where is_del != 1");
        sb.append(" and live_id = ? ");
        arrList.add(comment.getLive_id());
        int num = getListJdbcSize(sb.toString(), arrList);
        pageBean.setTotalCount(num);
        if (num > 0) {
            sb.append(" order by is_top desc,");
            if (comment.getOrder_type() == 1) {
                sb.append(" create_time desc");
            } else if (comment.getOrder_type() == 2) {
                sb.append(" fabulous_num desc");
            } else {
                sb.append(" create_time asc");
            }
            sb.append(" limit ?,?");
            arrList.add((pageBean.getCurPage() - 1) * pageBean.getPageSize());
            arrList.add(pageBean.getPageSize());
            pageBean.setMapList(getListJdbc(sb.toString(), arrList));
        }
        return pageBean;
    }

    /**
     * 点赞
     * @param id
     * @return
     */
    public int like(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_live_comment SET fabulous_num = fabulous_num + 1 WHERE id = ?");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 评论置顶取消置顶
     * @param id
     * @return
     */
    public int top(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" update t_live_comment set is_top = 1 - is_top where id = ?");
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 回复
     * @param id
     * @param reply
     * @param userId
     * @return
     */
    public int reply(int id, String reply, Integer userId) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" UPDATE t_live_comment SET update_time = now(),updater = ?,reply = ? WHERE id = ?");
        arrList.add(userId);
        arrList.add(reply);
        arrList.add(id);
        return updateJdbc(sb.toString(), arrList);
    }

    /**
     * 审核
     * @param id
     */
    public void auditComment(int id) {
        StringBuilder sb = new StringBuilder();
        List<Object> arrList = new ArrayList<Object>();
        sb.append(" update t_live_comment set status = 1 WHERE id = ?");
        arrList.add(id);
        updateJdbc(sb.toString(), arrList);
    }

}
